/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CollectionsView.java
 *
 * Created on 27.05.2013, 18:53:01
 */
package db21;

import db21.entities.Competences;
import db21.entities.Entities;
import db21.entities.Subjects;
import db21.entities.Themes;
import db21.entities.Collections;
import db21.entities.Collection;
import db21.entities.EducationalResources;
import db21.listeners.ClickOnlyListener;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JOptionPane;

/**
 *
 * @author Mamontov
 */
public class CollectionsView extends ApplicationPanel {

    Collection m_collection;
    
    public void updateWidgetsDueAChangedSelection() {
        if (m_collection != null) {
            txtName.setText(m_collection.name);
            Entities.setSelectedValues(lstAllResources, m_collection.resources);
            Entities.setSelectedValues(lstCompetenceResources, m_collection.resources);
            Entities.setSelectedValues(lstSubjectResource, m_collection.resources);
            Entities.setSelectedValues(lstThemeResources, m_collection.resources);          
        }
    }
    
    public Collection newCollection() {
        if (txtName.getText().isEmpty()) 
            return null;
        Collection t = new Collection();
        t.name = txtName.getText();
        int index = tabtabResources.getSelectedIndex();
        JList res;
        switch(index) {
            case 1:  res = lstThemeResources; break; 
            case 2:  res = lstCompetenceResources; break; 
            case 3:   res = lstSubjectResource; break;             
            default:  res = lstAllResources; break;  
        }
        t.resources = Entities.selectedItemsToEntities(res);
        
        return t;
    }
    
    /** Creates new form CollectionsView */
    public CollectionsView() {
        initComponents();
        JButton btns[] = {btnSeekResources, btnToResources, btnToMain };
        ApplicationMode modes[] = { 
            ApplicationMode.SeekResources,
            ApplicationMode.EducationalResources,
            ApplicationMode.MainForm
        };
        for(int i = 0; i < btns.length; i++) {
            this.bindOnClickListenerTogglingToMode(btns[i], modes[i]);
        }
        bindSave(btnSave);
        
        JList lists[] = { 
            lstAllCollections, 
            lstAllResources, 
            lstThemeResources, 
            lstSubjectResource,
            lstCompetenceResources
        };
        for(int i = 0 ; i < lists.length; i++) {
            DefaultListModel m = new DefaultListModel();
            lists[i].setModel(m);
        }
        
        btnDelete.addMouseListener(new ClickOnlyListener() {

            @Override
            public void mouseClicked(MouseEvent e) {
                JList c = CollectionsView.this.lstAllCollections;
                DefaultListModel k = (DefaultListModel)(CollectionsView.this.lstAllCollections.getModel());
                int si = c.getSelectedIndex();
                if (si != -1) {
                    String o = c.getSelectedValue().toString();
                    CollectionsView.this.m_model.resource(Collections.class).removeIndividual(o);
                    k.remove(si);
                    if (si < k.size()) {
                        c.setSelectedIndex(si);
                    }
                }
            }
        });
        
        btnAdd.addMouseListener(new ClickOnlyListener() {

            @Override
            public void mouseClicked(MouseEvent e) {
                Collection t = CollectionsView.this.newCollection();
                boolean ok = t!=null;
                if (ok) {
                    DefaultListModel k = (DefaultListModel)(CollectionsView.this.lstAllCollections.getModel());
                    boolean result = m_model.resource(Collections.class).add(t);
                    if (result) {
                        k.addElement(t.name);
                    } else {
                         JOptionPane.showMessageDialog(CollectionsView.this, "Individual already exists");
                    }
                }
                else {
                    JOptionPane.showMessageDialog(CollectionsView.this, "Please fill type and name");
                }
                        
            }
        });
        
    }
    
    @Override
    public void fireModelUpdate() {
        ArrayList<String> collections = this.m_model.resource(Collections.class).getList();
        Entities.populateListWithStrings(lstAllCollections, collections);
        
        ArrayList<String> resources = this.m_model.resource(EducationalResources.class).getList();
        Entities.populateListWithStrings(lstAllResources, resources);
        
        
        ArrayList<String> subjects = this.m_model.resource(Subjects.class).getList();
        Entities.populateComboBoxWithStrings(cmbSubject, subjects);
        
        ArrayList<String> competences = this.m_model.resource(Competences.class).getList();
        Entities.populateComboBoxWithStrings(cmbСompetence, competences);
        
        ArrayList<String> themes = this.m_model.resource(Themes.class).getList();
        Entities.populateComboBoxWithStrings(cmbTheme, themes);
        
        cmbThemeActionPerformed(null);
        cmbСompetenceActionPerformed(null);
        cmbSubjectActionPerformed(null);       
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnToMain = new javax.swing.JButton();
        btnToResources = new javax.swing.JButton();
        btnSeekResources = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        lstAllCollections = new javax.swing.JList();
        btnAdd = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        lblName = new javax.swing.JLabel();
        txtName = new javax.swing.JTextField();
        tabtabResources = new javax.swing.JTabbedPane();
        tabAllResources = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        lstAllResources = new javax.swing.JList();
        tabByTheme = new javax.swing.JPanel();
        cmbTheme = new javax.swing.JComboBox();
        lstAllResources1 = new javax.swing.JScrollPane();
        lstThemeResources = new javax.swing.JList();
        tabByCompetence = new javax.swing.JPanel();
        cmbСompetence = new javax.swing.JComboBox();
        lstAllResources2 = new javax.swing.JScrollPane();
        lstCompetenceResources = new javax.swing.JList();
        tabBySubjects = new javax.swing.JPanel();
        cmbSubject = new javax.swing.JComboBox();
        lstAllResources3 = new javax.swing.JScrollPane();
        lstSubjectResource = new javax.swing.JList();
        btnSave = new javax.swing.JButton();

        btnToMain.setText("К главному окну");

        btnToResources.setText("К ресурсам");

        btnSeekResources.setText("Поиск ресурсов");

        lstAllCollections.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        lstAllCollections.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                lstAllCollectionsValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(lstAllCollections);

        btnAdd.setText("Добавить");

        btnDelete.setText("Удалить");

        lblName.setText("Название");

        jScrollPane2.setViewportView(lstAllResources);

        javax.swing.GroupLayout tabAllResourcesLayout = new javax.swing.GroupLayout(tabAllResources);
        tabAllResources.setLayout(tabAllResourcesLayout);
        tabAllResourcesLayout.setHorizontalGroup(
            tabAllResourcesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(tabAllResourcesLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 359, Short.MAX_VALUE)
                .addContainerGap())
        );
        tabAllResourcesLayout.setVerticalGroup(
            tabAllResourcesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(tabAllResourcesLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 267, Short.MAX_VALUE)
                .addContainerGap())
        );

        tabtabResources.addTab("Все ресурсы", tabAllResources);

        cmbTheme.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbThemeActionPerformed(evt);
            }
        });

        lstAllResources1.setViewportView(lstThemeResources);

        javax.swing.GroupLayout tabByThemeLayout = new javax.swing.GroupLayout(tabByTheme);
        tabByTheme.setLayout(tabByThemeLayout);
        tabByThemeLayout.setHorizontalGroup(
            tabByThemeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, tabByThemeLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(tabByThemeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lstAllResources1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 359, Short.MAX_VALUE)
                    .addComponent(cmbTheme, javax.swing.GroupLayout.Alignment.LEADING, 0, 359, Short.MAX_VALUE))
                .addContainerGap())
        );
        tabByThemeLayout.setVerticalGroup(
            tabByThemeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(tabByThemeLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(cmbTheme, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lstAllResources1, javax.swing.GroupLayout.DEFAULT_SIZE, 241, Short.MAX_VALUE)
                .addContainerGap())
        );

        tabtabResources.addTab("По темам", tabByTheme);

        cmbСompetence.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbСompetenceActionPerformed(evt);
            }
        });

        lstAllResources2.setViewportView(lstCompetenceResources);

        javax.swing.GroupLayout tabByCompetenceLayout = new javax.swing.GroupLayout(tabByCompetence);
        tabByCompetence.setLayout(tabByCompetenceLayout);
        tabByCompetenceLayout.setHorizontalGroup(
            tabByCompetenceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, tabByCompetenceLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(tabByCompetenceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lstAllResources2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 359, Short.MAX_VALUE)
                    .addComponent(cmbСompetence, javax.swing.GroupLayout.Alignment.LEADING, 0, 359, Short.MAX_VALUE))
                .addContainerGap())
        );
        tabByCompetenceLayout.setVerticalGroup(
            tabByCompetenceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(tabByCompetenceLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(cmbСompetence, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lstAllResources2, javax.swing.GroupLayout.DEFAULT_SIZE, 241, Short.MAX_VALUE)
                .addContainerGap())
        );

        tabtabResources.addTab("По компетенциям", tabByCompetence);

        cmbSubject.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbSubjectActionPerformed(evt);
            }
        });

        lstAllResources3.setViewportView(lstSubjectResource);

        javax.swing.GroupLayout tabBySubjectsLayout = new javax.swing.GroupLayout(tabBySubjects);
        tabBySubjects.setLayout(tabBySubjectsLayout);
        tabBySubjectsLayout.setHorizontalGroup(
            tabBySubjectsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, tabBySubjectsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(tabBySubjectsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lstAllResources3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 359, Short.MAX_VALUE)
                    .addComponent(cmbSubject, javax.swing.GroupLayout.Alignment.LEADING, 0, 359, Short.MAX_VALUE))
                .addContainerGap())
        );
        tabBySubjectsLayout.setVerticalGroup(
            tabBySubjectsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(tabBySubjectsLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(cmbSubject, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lstAllResources3, javax.swing.GroupLayout.DEFAULT_SIZE, 241, Short.MAX_VALUE)
                .addContainerGap())
        );

        tabtabResources.addTab("По предметам", tabBySubjects);

        btnSave.setText("Сохранить");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnSave)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnDelete, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnSeekResources)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnToResources)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnToMain, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 351, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(lblName)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtName, javax.swing.GroupLayout.DEFAULT_SIZE, 332, Short.MAX_VALUE))
                    .addComponent(tabtabResources, javax.swing.GroupLayout.DEFAULT_SIZE, 384, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblName)
                            .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tabtabResources))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 264, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnAdd)
                            .addComponent(btnDelete)
                            .addComponent(btnSave))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnSeekResources)
                            .addComponent(btnToResources)
                            .addComponent(btnToMain))))
                .addContainerGap(20, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void cmbThemeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbThemeActionPerformed
        if (cmbTheme.getSelectedIndex() != -1) {
            // lstSubthemesOfTheme
            String subject = cmbTheme.getSelectedItem().toString();
            EducationalResources c = m_model.resource(EducationalResources.class);
            ArrayList<String> s = c.listForTheme(subject);
            Entities.populateListWithStrings(lstThemeResources, s);

            if (m_collection != null) {
                Entities.setSelectedValues(lstThemeResources, m_collection.resources);
            }
        }
    }//GEN-LAST:event_cmbThemeActionPerformed

    private void cmbСompetenceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbСompetenceActionPerformed
        if (cmbСompetence.getSelectedIndex() != -1) {
            // lstSubthemesOfTheme
            String subject = cmbСompetence.getSelectedItem().toString();
            EducationalResources c = m_model.resource(EducationalResources.class);
            ArrayList<String> s = c.listForCompetence(subject);
            Entities.populateListWithStrings(lstCompetenceResources, s);
            if (m_collection != null) {
                Entities.setSelectedValues(lstCompetenceResources, m_collection.resources);
            }
        }
    }//GEN-LAST:event_cmbСompetenceActionPerformed

    private void cmbSubjectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbSubjectActionPerformed
        if (cmbSubject.getSelectedIndex() != -1) {
            // lstSubthemesOfTheme
            String subject = cmbSubject.getSelectedItem().toString();
            EducationalResources c = m_model.resource(EducationalResources.class);
            ArrayList<String> s = c.listForSubject(subject);
            Entities.populateListWithStrings(lstSubjectResource, s);
            if (m_collection != null) {
                Entities.setSelectedValues(lstSubjectResource, m_collection.resources);
            }
        }
    }//GEN-LAST:event_cmbSubjectActionPerformed

    private void lstAllCollectionsValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_lstAllCollectionsValueChanged
        if (lstAllCollections.getSelectedIndex() != -1) {
            String data = lstAllCollections.getSelectedValue().toString();
            Collection r  = m_model.resource(Collections.class)
                                            .fetch(data);
            m_collection = r;
            updateWidgetsDueAChangedSelection();
        } else {
            m_collection = null;
        }
    }//GEN-LAST:event_lstAllCollectionsValueChanged

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnSave;
    private javax.swing.JButton btnSeekResources;
    private javax.swing.JButton btnToMain;
    private javax.swing.JButton btnToResources;
    private javax.swing.JComboBox cmbSubject;
    private javax.swing.JComboBox cmbTheme;
    private javax.swing.JComboBox cmbСompetence;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lblName;
    private javax.swing.JList lstAllCollections;
    private javax.swing.JList lstAllResources;
    private javax.swing.JScrollPane lstAllResources1;
    private javax.swing.JScrollPane lstAllResources2;
    private javax.swing.JScrollPane lstAllResources3;
    private javax.swing.JList lstCompetenceResources;
    private javax.swing.JList lstSubjectResource;
    private javax.swing.JList lstThemeResources;
    private javax.swing.JPanel tabAllResources;
    private javax.swing.JPanel tabByCompetence;
    private javax.swing.JPanel tabBySubjects;
    private javax.swing.JPanel tabByTheme;
    private javax.swing.JTabbedPane tabtabResources;
    private javax.swing.JTextField txtName;
    // End of variables declaration//GEN-END:variables
}
